package com.group01.community.controller;

import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group01.community.entity.ZyVisitor;
import com.group01.community.service.ZyVisitorService;
import com.group01.community.utils.ResponseResult;
import com.group01.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 访客邀请  前端控制器
 * </p>
 *
 * @author iuoly
 * @since 2022-07-04
 */
@Api(tags = "访客邀请")
@RestController
@RequestMapping("/zyVisitor")
public class ZyVisitorController {

    @Autowired
    private ZyVisitorService zyVisitorService;


    @ApiOperation("根据小区名称和访客信息分页查询")
    @GetMapping
    public ResponseResult<IPage<ZyVisitor>> getZyVisitorByType(Long current, Long pageSize, ZyVisitor zyVisitor) {
        IPage<ZyVisitor> data = zyVisitorService.selectAllByCommunityName(current, pageSize, zyVisitor);
        return ResponseResult.ok(data);
    }


    //表格导出接口
    @ApiOperation("访客导出")
    @GetMapping("/export")
    public void export(HttpServletResponse response) {
        //查询所有用户
        List<ZyVisitor> all = zyVisitorService.selectZyVisitor();
        //在内存操作，写到浏览器
        ExcelWriter writer = ExcelUtil.getWriter(true);
        try {
            //自定义标题别名
            writer.addHeaderAlias("visitorId", "id");
            writer.addHeaderAlias("communityId", "小区id");
            writer.addHeaderAlias("visitorName", "访客姓名");
            writer.addHeaderAlias("visitorPhoneNumber", "访客手机号");
            writer.addHeaderAlias("createTime", "创建时间");
            writer.addHeaderAlias("visitorDate", "到访时间");
            writer.addHeaderAlias("createById", "创建人id");
            writer.addHeaderAlias("updateTime", "更新时间");
            writer.addHeaderAlias("updateBy", "更新者");
            writer.addHeaderAlias("remark", "备注");
            writer.addHeaderAlias("communityName", "小区名称");

            //默认配置
            writer.write(all, true);
            //设置content—type
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset:utf-8");

            //设置标题
            String fileName = URLEncoder.encode("用户信息", "UTF-8");
            //Content-disposition是MIME协议的扩展，MIME协议指示MIME用户代理如何显示附加的文件。
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            ServletOutputStream outputStream = response.getOutputStream();

            //将Writer刷新到OutPut
            writer.flush(outputStream, true);
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            writer.close();
        }
    }


    @ApiOperation("填写访客邀请信息登记 小程序")
    @PostMapping
    public ResponseResult<String> addZyVisitor(@RequestBody ZyVisitor zyVisitor) {
        zyVisitor.setVisitorId(UUIDUtils.getId());
        zyVisitor.setCreateTime(LocalDateTime.now());

        zyVisitorService.save(zyVisitor);
        return ResponseResult.ok("ok");
    }

    @ApiOperation("访客邀请 审核修改")
    @PutMapping
    public ResponseResult<String> updateZyVisitor(@RequestBody ZyVisitor zyVisitor) {
        zyVisitorService.updateById(zyVisitor);
        zyVisitor.setUpdateTime(LocalDateTime.now());
        return ResponseResult.ok("ok");
    }

    @ApiOperation("删除访客记录")
    @DeleteMapping("/{visitorId}")
    public ResponseResult<String> deleteZyVisitor(@PathVariable String visitorId) {
        zyVisitorService.removeById(visitorId);
        return ResponseResult.ok("ok");
    }



//
//    @ApiOperation("查询访客邀请信息")
//    @GetMapping("/page/{current}/{size}")
//    public ResponseResult<IPage< ZyVisitor>> getZyVisitors(@PathVariable Long current, @PathVariable Long size){
////        System.out.println("current = " + current);
////        System.out.println("size = " + size);
//        IPage<ZyVisitor> data = zyVisitorService.zyVisitorPage(current, size);
////        System.out.println(data.getRecords());
//        return ResponseResult.ok(data);
//    }


//    @ApiOperation("根据小区名称和访客信息查询")
//    @GetMapping
//    public ResponseResult<IPage<ZyVisitor>> getZyVisitorByType(Long current, Long pageSize, String communityName, String visitorName, String visitorPhoneNumber, String visitorDate) {
//        ZyVisitor zyVisitor = new ZyVisitor();
//
//        zyVisitor.setCommunityName(communityName);
//        zyVisitor.setVisitorPhoneNumber(visitorPhoneNumber);
//        zyVisitor.setVisitorName(visitorName);
////        zyVisitor.setVisitorDate(visitorDate);
//
//        IPage<ZyVisitor> data = zyVisitorService.selectAllByCommunityName(current, pageSize, communityName, zyVisitor);
//        return ResponseResult.ok(data);
//    }


    //post传单个对象

//    axios({
//        method: 'post',
//                url: 'http://localhost:8080/zyVisitor/',
//                data: {
//            communityName:this.value,
//                    visitorName:this.formVisitors.visitorName,
//                    visitorPhoneNumber:this.formVisitors.visitorPhoneNumber,
//                    visitorDate:this.formVisitors.visitorDate,
//        }
//    }).then(resp => {
//        console.log(152);
//        console.log(resp)
//    })

//    @ApiOperation("根据小区名称和访客信息查询")
//    @PostMapping//参数按顺序
//    public ResponseResult<List<ZyVisitor>> getZyVisitorByType(String communityName ,@RequestBody ZyVisitor zyVisitor){
//        System.out.println("communityName = " + communityName);
//        System.out.println("zyVisitor = " + zyVisitor);
//        return ResponseResult.ok(zyVisitorService.selectAllByCommunityName("恒大城三期", zyVisitor));
//    }


//纯字符串拼接
//    @ApiOperation("根据小区名称和访客信息查询")
//    @GetMapping("/{communityName}/{visitorName}/{visitorDate}/{visitorPhoneNumber}")//参数按顺序
//    public ResponseResult<List<ZyVisitor>> getZyVisitorByType(@PathVariable  String communityName,@PathVariable String visitorName,@PathVariable String visitorDate,@PathVariable String visitorPhoneNumber){
//        System.out.println("communityName = " + communityName);
//        System.out.println("visitorName = " + visitorName);
//        System.out.println("visitorDate = " + visitorDate);
//        System.out.println("visitorPhoneNumber = " + visitorPhoneNumber);
//
//        ZyVisitor zyVisitor = new ZyVisitor();
//        zyVisitor.setVisitorName(visitorName);
//
//        LocalDate parseLocalDate = LocalDate.parse(visitorDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
//        System.out.println("parseLocalDate = " + parseLocalDate);
//        zyVisitor.setVisitorDate(parseLocalDate);
//
//        zyVisitor.setVisitorPhoneNumber(visitorPhoneNumber);
//        return ResponseResult.ok(zyVisitorService.selectAllByCommunityName(communityName, zyVisitor));
//    }
//

}

